package com.zzc.utils;

import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.Random;


@Component
public class MD5Util {

    private final String salt = "1a2b3c4d";

    public String pwdToFormPwd(String pwd) {

        String str = salt.charAt(0) + salt.charAt(2) + pwd + salt.charAt(5) + salt.charAt(4);
        return DigestUtils.md5DigestAsHex(str.getBytes());

    }

    public String pwdToDbPwd(String pwd) {

        String formPwd = pwdToFormPwd(pwd);
        String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 7; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        String st = sb.toString();
        String str = st.charAt(0) + st.charAt(2) + formPwd + st.charAt(5) + st.charAt(4);
        System.out.println(st);
        return DigestUtils.md5DigestAsHex(str.getBytes());
    }

    public String pwdToDbPwd(String pwd, String userSalt){
        String formPwd = pwdToFormPwd(pwd);
        String str = userSalt.charAt(0) + userSalt.charAt(2) + formPwd + userSalt.charAt(5) + userSalt.charAt(4);
        return DigestUtils.md5DigestAsHex(str.getBytes());
    }

}
